package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.bo.WxAuthRegisterBo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.WxAuthRegisterVo;
import com.cskaoyan.bean.vo.WxAuthResetBo;
import com.cskaoyan.controller.system.annotation.RecordOperation;
import com.cskaoyan.service.wx.WxAuthService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author jzp
 * @since 2022/11/22 13:51
 */
@RestController
@RequestMapping("wx/auth/")
public class WxAuth {
    @Autowired
    WxAuthService wxAuthService;


    @RequestMapping("login")
    public BaseRespVo login(@RequestBody Map map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");

        return wxAuthService.login(username, password);
    }

    @RequestMapping("logout")
    public BaseRespVo logout() {
        SecurityUtils.getSubject().logout();
        return BaseRespVo.succeed("成功");
    }

    @RequestMapping("regCaptcha")
    public BaseRespVo regCaptcha(@RequestBody Map map) {
        String mobile = (String) map.get("mobile");
        // {"errno":0,"errmsg":"成功"}
        // {"errno":402,"errmsg":"参数值不对"}
        boolean flag = wxAuthService.regCaptcha(mobile);
        if (!flag) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(402);
            baseRespVo.setErrmsg("参数值不对");
            return baseRespVo;
        }
        return BaseRespVo.succeed();
    }

    @RequestMapping("register")
    public BaseRespVo register(@RequestBody WxAuthRegisterBo wxAuthRegisterBo, HttpServletRequest request) {
        // {"errno":703,"errmsg":"验证码错误"}
        // {"errno":0,"data":{"userInfo":{"nickName":"mycs123","avatarUrl":"https://yanxuan.nosdn.127.net/80841d741d7fa3073e0ae27bf487339f.jpg?imageView&quality=90&thumbnail=64x64"},"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0aGlzIGlzIG1hcmtldCB0b2tlbiIsImF1ZCI6Ik1JTklBUFAiLCJpc3MiOiJNQVJLRVQiLCJleHAiOjE2NjkyMTIzNDcsInVzZXJJZCI6MTEsImlhdCI6MTY2OTIwNTE0N30.KflXFfxDGTxn6XG5TX7JFBkHw0-c_3JlrZWtDiokh7A"},"errmsg":"成功"}
        // {"errno":705,"errmsg":"手机号已注册"}
        WxAuthRegisterVo wxAuthRegisterVo = wxAuthService.register(wxAuthRegisterBo, request);
        if (wxAuthRegisterVo == null) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(703);
            baseRespVo.setErrmsg("验证码错误");
            return baseRespVo;
        }
        if ("用户名重复".equals(wxAuthRegisterVo.getToken())) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(704);
            baseRespVo.setErrmsg("用户名已注册");
            return baseRespVo;
        }
        if ("手机号已经注册".equals(wxAuthRegisterVo.getToken())) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(705);
            baseRespVo.setErrmsg("手机号已经注册");
            return baseRespVo;
        }
        return BaseRespVo.succeed(wxAuthRegisterVo);
    }

    @RequestMapping("reset")
    public BaseRespVo reset(@RequestBody WxAuthResetBo wxAuthRegisterBo) {
        // {"errno":706,"errmsg":"手机号未注册"}
        // {"errno":0,"errmsg":"成功"}
        // {"errno":703,"errmsg":"验证码错误"}
        Integer code = wxAuthService.reset(wxAuthRegisterBo);
        if (code == null) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(706);
            baseRespVo.setErrmsg("手机号未注册");
            return baseRespVo;
        }
        if (code == 0) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(703);
            baseRespVo.setErrmsg("验证码错误");
            return baseRespVo;
        }
        return BaseRespVo.succeed();
        // return null;
    }
}
